var modal = $(".cd-user-modal");
$(function () {

	$(".js-show-menu").click(function () {
        $(this).find("ul").css({"display":"block"});
    });

    /**
     * 登陆
     */
	$(".cd-signin,.js-user-login").click(function () {
        login_block();
    });

    /**
     * 注册
     */
    $(".cd-signup,.js-open-register").click(function () {
        register_block();
    });

    /**
     * 忘记密码
     */
    $(".js-forget-passward").click(function () {
        forgetpwd_block();
    });

    /**
     * 关闭
     */
    $(".cd-close-form").click(function () {
        modal.removeClass("is-visible");
        modal.find("#cd-signup").css({"display":"none"});
        modal.find("#cd-forgetpwd").css({"display":"none"});
        modal.find("#cd-login").css({"display":"none"});
    });

	function login_block() {
	    $(".js-show-menu >ul").css({"display":"none"});
        modal.addClass("is-visible");
        modal.find("#cd-signup").css({"display":"none"});
        modal.find("#cd-forgetpwd").css({"display":"none"});
        modal.find("#cd-login").css({"display":"block"});
    }

    function register_block() {
        $(".js-show-menu >ul").css({"display":"none"});
        modal.addClass("is-visible");
        modal.find("#cd-login").css({"display":"none"});
        modal.find("#cd-forgetpwd").css({"display":"none"});
        modal.find("#cd-signup").css({"display":"block"});
    }

    function forgetpwd_block() {
        $(".js-show-menu >ul").css({"display":"none"});
        modal.addClass("is-visible");
        modal.find("#cd-login").css({"display":"none"});
        modal.find("#cd-signup").css({"display":"none"});
        modal.find("#cd-forgetpwd").css({"display":"block"});
    }

    $(document).keydown(function (e) {
        if (e.keyCode == 13 && modal.find("#cd-login").css("display")=='block') {
            login_check();
        }
    });
});

/**
 * 登录检查
 */
function login_check() {
    layui.use('layer', function(){
        var layer = layui.layer;
        var username = $("#username").val();
        var password = $("#password").val();
        if (!username) {
            layer.msg("请输入用户名",{icon:5});
            return false;
        } else if (!password) {
            layer.msg("请输入密码",{icon:5});
            return false;
        } else {
            $.ajax({
                url: '/login',
                type: 'post',
                data: {'username': username, 'password': password},
                dataType: 'json',
                success: function (d) {
                    if (d.status==200) {
                        window.location.reload();
                    } else {
                        layer.msg(d.message?d.message:"登录失败",{icon:5});
                    }
                }
            });
        }
    });
}

/**
 * 注册账号
 */
function register_check() {
    layui.use('layer', function(data){
        var layer = layui.layer;
        $(this).attr("disabled","disabled");
        var time = 1200;
        var username = $("#register_username").val();
        var password = $("#register_password").val();
        var email = $("#register_email").val();
        if (!username) {
            layer.msg("请输入用户名!", {icon: 5,time:time});
            setTimeout(function () {
                $(this).removeAttr("disabled");
            },time)
            return;
        }
        if (!password) {
            layer.msg("请输入密码!", {icon: 5,time:time});
            setTimeout(function () {
                $(this).removeAttr("disabled");
            },time)
            return;
        }
        if (!password || password.length<6) {
            layer.msg("密码太简短!", {icon: 5,time:time});
            setTimeout(function () {
                $(this).removeAttr("disabled");
            },time)
            return;
        }
        var pwd_reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,32}$/;
        if (!pwd_reg.test(password)) {
            layer.msg("请输入正确的密码!", {icon: 5});
            return false;
        }
        if (!email) {
            layer.msg("请输入邮箱!", {icon: 5,time:time});
            setTimeout(function () {
                $(this).removeAttr("disabled");
            },time)
            return;
        }
        var email_reg = /^[\w-]+(.[\w-]+)*@[\w-]+(.[\w-]+)+$/;
        if (!email_reg.test(email)) {
            layer.msg("请填写正确的邮箱!", {icon: 5});
            return false;
        }
        if (!checkCode(email)) {
            layer.msg("验证码错误!", {icon: 5,time:time});
            setTimeout(function () {
                $(this).removeAttr("disabled");
            },time)
            return;
        }

        $.ajax({
            url: '/register',
            type: 'post',
            data: {'username': username, 'password': password,"email":email},
            dataType: 'json',
            success: function (data) {
                if (data.status==0) {
                    layer.msg("注册成功~~~",{icon:6});
                    $(".cd-user-modal").removeClass("is-visible");
                }else{
                    layer.msg(data.msg?data.msg:"注册失败~~~",{icon:6});
                }
            }
        });
    })

}

/**
 * 找回密码
 */
function forgetpwd_check() {
    layui.use('layer', function(data){
        var layer = layui.layer;
        $(this).attr("disabled","disabled");
        var time = 1200;
        var username = $("#forget_username").val();
        var email = $("#forget_email").val();
        if (!username) {
            layer.msg("请输入用户名!", {icon: 5,time:time});
            setTimeout(function () {
                $(this).removeAttr("disabled");
            },time)
            return;
        }
        if (!email) {
            layer.msg("请输入邮箱!", {icon: 5,time:time});
            setTimeout(function () {
                $(this).removeAttr("disabled");
            },time)
            return;
        }
        var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        if (!myreg.test(email)) {
            layer.msg("请填写正确的邮箱!", {icon: 5});
            return false;
        }

        var emails = [];
        emails.push(email);
        $.ajax({
            url: ("/u/forget/pwd"),
            type: "post",
            data:{"username":username,"email":JSON.stringify(emails)},
            dataType: "json",
            async:false,
            success: function (data) {
                if (data.code==0) {
                    layer.msg(data.msg?data.msg:"密码发送至邮箱成功~",{icon:6});
                    window.location.reload();
                } else {
                    layer.msg(data.msg?data.msg:"发送邮件失败~",{icon:5});
                }
            },
            error: function (data) {
                layer.msg(data.msg?data.msg:"发送邮件失败~",{icon:5});
            }
        });
    })
}





